package dd.lo.chapter2;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

/**
 * 对应程序示例
 * Example 2-9. Getting and setting pixels in Example 2-8
 */
public class Example5 {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取一张图片
        Mat srcImg = Imgcodecs.imread("/Users/kwok/Downloads/男女骑友.png", Imgcodecs.IMREAD_UNCHANGED);
        if (srcImg.empty()) {
            throw new RuntimeException("图片读取失败");
        }
        //读取原图的像素点，有BGR三个维度
        int x = 16, y = 32;
        double[] srcPixel = srcImg.get(x, y);
        System.out.printf("Origin image pixel at (%d, %d) is [%f, %f, %f]\n", x, y, srcPixel[0], srcPixel[1], srcPixel[2]);
        Mat grayOut = new Mat();
        Imgproc.cvtColor(srcImg, grayOut, Imgproc.COLOR_BGR2GRAY);
        //读取灰度图的像素点，只有一个灰度的维度
        double[] grayPixel = grayOut.get(x, y);
        System.out.printf("Gray image pixel at (%d, %d) is [%f]\n", x, y, grayPixel[0]);
        //读取向下取样图的像素点，对应位置要除4，因为长和宽都缩小为四分之一
        x /= 4;
        y /= 4;
        Mat pyrDownOut = new Mat();
        Imgproc.pyrDown(srcImg, pyrDownOut);
        double[] pyrDownPixel = pyrDownOut.get(x, y);
        System.out.printf("Gray image pixel at (%d, %d) is [%f, %f, %f]\n", x, y, pyrDownPixel[0], pyrDownPixel[1], pyrDownPixel[2]);
    }
}
